package com.jt.tenant.activity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.action.QiNiuPictureUpload;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.dialog.LoadDialog;
import com.zcx.helper.activity.AppPictureActivity;

import java.io.File;

/**
 * Created by DR on 2016/2/26.
 */
public class ChoosePicActivity extends AppPictureActivity implements View.OnClickListener {

    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1234;

    private TextView go_photo, go_album;

    private LinearLayout bg, bg1;

    private ImageView close;

    public static getImgPathListener getImgPathListener;

    private int type = 0;//0:选择头像，1:开通高速服务申请

    @Override
    public void setContentView(int layoutResID) {

        super.setContentView(layoutResID);

        JTApplication.ScaleScreenHelper.loadView((ViewGroup) getWindow().getDecorView());

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_choose_head_pic);

        type = Integer.valueOf(getIntent().getStringExtra("type"));

        initView();

    }

    private void initView() {

        (go_photo = (TextView) findViewById(R.id.id_confirm)).setOnClickListener(this);

        (go_album = (TextView) findViewById(R.id.id_cancel)).setOnClickListener(this);

        (bg = (LinearLayout) findViewById(R.id.bg)).setOnClickListener(this);

        (bg1 = (LinearLayout) findViewById(R.id.bg1)).setOnClickListener(this);

        (close = (ImageView)findViewById(R.id.iv_dialog_close)).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.bg:

                finish();

                break;
            case R.id.bg1:

                break;
            //拍照
            case R.id.id_confirm:

                startCamera(null);

                break;
            //相册
            case R.id.id_cancel:

                startAlbum(null);

                break;

            case R.id.iv_dialog_close:

                finish();

                break;

        }

    }

    @Override
    protected String getCameraAbsolutePath() {

        return Environment.getExternalStorageDirectory().getAbsolutePath() + "/jtf";

    }

    @Override
    protected void resultPhotoPath(ImageView imageView, final String photoPath) {

        Log.e("TAG", "path2==" + photoPath);

        bg1.setVisibility(View.GONE);

        new LoadDialog(ChoosePicActivity.this).show();

        QiNiuPictureUpload.upload(new File(photoPath), new QiNiuPictureUpload.OnPictureUploadCallBack() {

            @Override
            public void onSuccess(final String imageUrl) {

                finish();

                switch (type) {

                    case 0:

                        if (PersonInfoActivity.setHeadPicListener != null) {

                            PersonInfoActivity.setHeadPicListener.setHeadPic("http://7xrdxm.com2.z0.glb.qiniucdn.com/" + imageUrl);

                        }

                        getImgPathListener = new getImgPathListener() {

                            @Override
                            public String getImgPath() {

                                return "http://7xrdxm.com2.z0.glb.qiniucdn.com/" + imageUrl;

                            }

                        };

                        break;

                    case 1:

                        UpPictureActivity.mRefrenceListener.setRefrencePic("http://7xrdxm.com2.z0.glb.qiniucdn.com/" + imageUrl);

                        break;

                }

            }

        });

    }

    @Override
    protected Crop getCrop() {

        if(type == 0) {

            return new Crop().setCrop(true);

        }else {

            return new Crop().setCrop(false);

        }

    }

    /***
     * 得到图片路径  将图片路径传给后台
     */
    public abstract class getImgPathListener {

        public abstract String getImgPath();

    }

}

